package com.jintoufs.service.baseinfo.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jhj.common.redis.JhjRedis;
import com.jintoufs.dao.baseinfo.TickerDAO;
import com.jintoufs.domain.baseinfo.Ticker;
import com.jintoufs.service.baseinfo.MarketService;
import com.jintoufs.util.http.CoinwHttpsUtil;
import com.jintoufs.util.http.HttpClientUtils;
import com.jintoufs.util.http.HttpRequest;
import com.jintoufs.util.http.HttpsClientUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Repository
public class MarketServiceImpl implements MarketService {
    @Resource
    private TickerDAO tickerDAO;
    @Resource
    private JhjRedis jhjRedis;


    @Override
    public void livecoin(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "https://api.livecoin.net/exchange/ticker";
        String json = HttpRequest.sendGet(api, null);
        Integer siteId = Integer.parseInt(site.get("id").toString());
        try {
            if (!StringUtils.isEmpty(json)) {
                List<JSONObject> array = JSON.parseArray(json, JSONObject.class);
                for (JSONObject tickerObject : array) {
                    Ticker ticker = new Ticker();
                    String code = tickerObject.get("cur").toString();
                    Integer coinId = 0;
                    for (Map<String, Object> map : coins) {
                        if (code.equalsIgnoreCase(map.get("code").toString())) {
                            coinId = Integer.parseInt(map.get("id").toString());
                            break;
                        }
                    }
                    if (coinId == 0) {
                        continue;
                    }
                    String symbol = tickerObject.get("symbol").toString();
                    String unitCode = symbol.split("/")[1];
                    if (unitCode.equals("RUR")) {
                        continue;
                    }
                    ticker.setUnitCode(unitCode);
                    if (tickerObject.get("best_bid") != null) {
                        String buy = tickerObject.get("best_bid").toString();
                        ticker.setBuy(Double.parseDouble(buy));
                    }
                    if (tickerObject.get("best_ask") != null) {
                        String sell = tickerObject.get("best_ask").toString();
                        ticker.setSell(Double.parseDouble(sell));
                    }
                    if (tickerObject.get("low") != null) {
                        String low = tickerObject.get("low").toString();
                        ticker.setLow(Double.parseDouble(low));
                    }
                    if (tickerObject.get("high") != null) {
                        String high = tickerObject.get("high").toString();
                        ticker.setHigh(Double.parseDouble(high));
                    }
                    if (tickerObject.get("last") != null) {
                        String last = tickerObject.get("last").toString();
                        ticker.setLast(Double.parseDouble(last));
                    }
                    if (tickerObject.get("volume") != null) {
                        String vol = tickerObject.get("volume").toString();
                        ticker.setVol(Double.parseDouble(vol));
                    }
                    ticker.setCoinId(coinId);
                    ticker.setSiteId(siteId);
                    if (ticker.getLast().doubleValue() != 0D) {
                        tickerDAO.insert(ticker);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void bithumb(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "https://api.bithumb.com/public/ticker/all";
        String json = HttpRequest.sendGet(api, null);
        Integer siteId = Integer.parseInt(site.get("id").toString());
        try {
            if (!StringUtils.isEmpty(json)) {
                JSONObject object = JSON.parseObject(json);
                for (Map<String, Object> coin : coins) {
                    String code = coin.get("code").toString();
                    Ticker ticker = new Ticker();
                    ticker.setUnitCode("KRW");

                    if (object.get("data") == null || "".equals(object.get("data"))) {
                        continue;
                    }
                    JSONObject tickerObject = null;
                    JSONObject coinObject = (JSONObject) object.get("data");

                    if (coin.get("code") != null) {
                        String rename = coin.get("code").toString();
                        //用别名
                        tickerObject = (JSONObject) coinObject.get(rename);
                        //不用别名
                    } else {
                        tickerObject = (JSONObject) coinObject.get(code);
                    }

                    if (tickerObject.get("buy_price") != null) {
                        String buy = tickerObject.get("buy_price").toString();
                        ticker.setBuy(Double.parseDouble(buy));
                    }
                    if (tickerObject.get("sell_price") != null) {
                        String sell = tickerObject.get("sell_price").toString();
                        ticker.setSell(Double.parseDouble(sell));
                    }
                    if (tickerObject.get("min_price") != null) {
                        String low = tickerObject.get("min_price").toString();
                        ticker.setLow(Double.parseDouble(low));
                    }
                    if (tickerObject.get("max_price") != null) {
                        String high = tickerObject.get("max_price").toString();
                        ticker.setHigh(Double.parseDouble(high));
                    }
                    if (tickerObject.get("closing_price") != null) {
                        String last = tickerObject.get("closing_price").toString();
                        ticker.setLast(Double.parseDouble(last));
                    }
                    if (tickerObject.get("volume_1day") != null) {
                        String vol = tickerObject.get("volume_1day").toString();
                        ticker.setVol(Double.parseDouble(vol));
                    }
                    ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                    ticker.setSiteId(siteId);
                    tickerDAO.insert(ticker);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void cexcom(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String api = "http://api.cex.com/api/v1/ticker.do?symbol=";
        String[] unitCodes = {"btc", "usdt", "eth"};
        try {
            for (String unitCode : unitCodes) {
                for (Map<String, Object> coin : coins) {
                    String coinName = coin.get("code").toString();
                    api += coinName.toLowerCase() + "_" + unitCode;
                    String json = HttpRequest.sendGet(api, null);
                    JSONObject object = JSONObject.parseObject(json);
                    JSONObject tickerObject = object.getJSONObject("ticker");

                    Ticker ticker = new Ticker();

                    unitCode = unitCode.replace("usdt", "usd");
                    ticker.setUnitCode(unitCode);
                    if (tickerObject.get("buy") != null) {
                        String buy = tickerObject.get("buy").toString();
                        ticker.setBuy(Double.parseDouble(buy));
                    }
                    if (tickerObject.get("sell") != null) {
                        String sell = tickerObject.get("sell").toString();
                        ticker.setSell(Double.parseDouble(sell));
                    }
                    if (tickerObject.get("low") != null) {
                        String low = tickerObject.get("low").toString();
                        ticker.setLow(Double.parseDouble(low));
                    }
                    if (tickerObject.get("high") != null) {
                        String high = tickerObject.get("high").toString();
                        ticker.setHigh(Double.parseDouble(high));
                    }
                    if (tickerObject.get("last") != null) {
                        String last = tickerObject.get("last").toString();
                        ticker.setLast(Double.parseDouble(last));
                    }
                    if (tickerObject.get("vol") != null) {
                        String vol = tickerObject.get("vol").toString();
                        ticker.setVol(Double.parseDouble(vol));
                    }
                    ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                    ticker.setSiteId(siteId);
                    tickerDAO.insert(ticker);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     *
     * liqui
     */
    @Override
    public void liqui(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] units = new String[]{"btc", "eth", "usdt"};
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                for (String unit : units) {

                    String api = "https://api.liqui.io/api/3/ticker/COIN_UNIT";
                    api = api.replace("COIN", coin.get("code").toString());
                    api = api.replace("UNIT", unit);
                    api = api.toLowerCase();
                    json = HttpsClientUtils.doGet(api);
                    if (json != null && !"".equals(json)) {
                        JSONObject object2 = JSON.parseObject(json);
                        JSONObject object = object2.getJSONObject(coin.get("code").toString().toLowerCase() + "_" + unit);
                        if (object != null) {
                            String high = object.get("high").toString();
                            String low = object.get("low").toString();
                            String vol = object.get("vol").toString();
                            String buy = object.get("buy").toString();
                            String sell = object.get("sell").toString();
                            String last = object.get("last").toString();
                            Ticker ticker = new Ticker();
                            ticker.setHigh(Double.parseDouble(high));
                            ticker.setLow(Double.parseDouble(low));
                            ticker.setVol(Double.parseDouble(vol));
                            ticker.setBuy(Double.parseDouble(buy));
                            ticker.setSell(Double.parseDouble(sell));
                            ticker.setLast(Double.parseDouble(last));
                            String unitCode = unit.substring(0, 3);
                            ticker.setUnitCode(unitCode.toUpperCase());
                            ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                            ticker.setSiteId(siteId);
                            tickerDAO.insert(ticker);
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void coinw(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                if(coin.get("price_api")==null||"".equals(coin.get("price_api").toString())){
                    continue;
                }
                String api = coin.get("price_api").toString();
                json = CoinwHttpsUtil.getStringByHttpsPost(api);
                if("error".equals(json)){continue;}
                JSONObject object = JSON.parseObject(json);
                if (object.get("code").toString().equals("200")) {
                    String high = object.get("high").toString();
                    String low = object.get("high").toString();
                    String vol = object.get("vol").toString();
                    String buy = object.get("buy").toString();
                    String sell = object.get("sell").toString();
                    String data = object.get("data").toString();
                    JSONObject lastObject = JSON.parseObject(data);
                    String last = lastObject.get("last").toString();
                    Ticker ticker = new Ticker();
                    ticker.setHigh(Double.parseDouble(high));
                    ticker.setLow(Double.parseDouble(low));
                    ticker.setVol(Double.parseDouble(vol));
                    ticker.setBuy(Double.parseDouble(buy));
                    ticker.setSell(Double.parseDouble(sell));
                    ticker.setLast(Double.parseDouble(last));
                    ticker.setUnitCode("CNY");
                    ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                    ticker.setSiteId(siteId);
                    tickerDAO.insert(ticker);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void bitstamp(Map<String, Object> site, List<Map<String, Object>> coins) {
        Ticker ticker = null;
        String json = "";
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String api = "https://www.bitstamp.net/api/v2/ticker/";
        String[] unitCodes = {"btc", "usd", "eth"};
        try {
            for (String unitCode : unitCodes) {
                for (Map<String, Object> coin : coins) {
                    String coinName = coin.get("code").toString();
                    api += coinName + "_" + unitCode;
                    json = HttpRequest.sendGet(api, null);
                    JSONObject object = JSON.parseObject(json);
                    String high = object.get("high").toString();
                    String last = object.get("last").toString();
                    String low = object.get("low").toString();
                    String vol = object.get("volume").toString();
                    String buy = object.get("bid").toString();
                    String sell = object.get("ask").toString();
                    ticker = new Ticker();
                    ticker.setHigh(Double.parseDouble(high));
                    ticker.setLow(Double.parseDouble(low));
                    ticker.setVol(Double.parseDouble(vol));
                    ticker.setBuy(Double.parseDouble(buy));
                    ticker.setSell(Double.parseDouble(sell));
                    ticker.setLast(Double.parseDouble(last));
                    ticker.setUnitCode(unitCode);
                    ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                    ticker.setSiteId(siteId);
                    tickerDAO.insert(ticker);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     *
     * bitfinex
     */
    @Override
    public void bitfinex(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "https://api.bitfinex.com/v1/tickers";
        String  json = HttpsClientUtils.doGet(api);
        //json= HttpsClientUtils.doPost(api);
        Integer siteId = Integer.parseInt(site.get("id").toString());
        try {
            if (!StringUtils.isEmpty(json)) {
                List<JSONObject> array = JSON.parseArray(json, JSONObject.class);
                for (JSONObject tickerObject : array) {
                    Ticker ticker = new Ticker();
                    String symbol = tickerObject.get("pair").toString();
                    String unitCode = symbol.substring(symbol.length() - 3, symbol.length());
                    String code = symbol.substring(0, symbol.length() - 3);
                    Integer coinId = 0;
                    for (Map<String, Object> map : coins) {
                        if (code.equalsIgnoreCase(map.get("code").toString())) {
                            coinId = Integer.parseInt(map.get("id").toString());
                            break;
                        }
                    }
                    if (coinId == 0) {
                        continue;
                    }

                    ticker.setUnitCode(unitCode);
                    if (tickerObject.get("bid") != null) {
                        String buy = tickerObject.get("bid").toString();
                        ticker.setBuy(Double.parseDouble(buy));
                    }
                    if (tickerObject.get("ask") != null) {
                        String sell = tickerObject.get("ask").toString();
                        ticker.setSell(Double.parseDouble(sell));
                    }
                    if (tickerObject.get("low") != null) {
                        String low = tickerObject.get("low").toString();
                        ticker.setLow(Double.parseDouble(low));
                    }
                    if (tickerObject.get("high") != null) {
                        String high = tickerObject.get("high").toString();
                        ticker.setHigh(Double.parseDouble(high));
                    }
                    if (tickerObject.get("last_price") != null) {
                        String last = tickerObject.get("last_price").toString();
                        ticker.setLast(Double.parseDouble(last));
                    }
                    if (tickerObject.get("volume") != null) {
                        String vol = tickerObject.get("volume").toString();
                        ticker.setVol(Double.parseDouble(vol));
                    }
                    ticker.setCoinId(coinId);
                    ticker.setSiteId(siteId);
                    if (ticker.getLast().doubleValue() != 0D) {
                        tickerDAO.insert(ticker);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //TODO 返回403，需要验证码
    @Override
    public void poloniex(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "https://poloniex.com/public?command=returnTicker";
        String json = HttpsClientUtils.doPostSSL(api, null);
        Integer siteId = Integer.parseInt(site.get("id").toString());
        try {
            if (!StringUtils.isEmpty(json)) {
                Map jsonMap = (Map) net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(json), Map.class);
                Iterator<Map.Entry<String, Map>> iterator = jsonMap.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, Map> entry = iterator.next();
                    String symbol = entry.getKey();
                    Map tickerObject = entry.getValue();
                    Ticker ticker = new Ticker();
                    Integer coinId = 0;
                    String code = symbol.split("_")[1];
                    String unitCode = symbol.split("_")[0];
                    for (Map<String, Object> map : coins) {
                        if (code.equalsIgnoreCase(map.get("code").toString())) {
                            coinId = Integer.parseInt(map.get("id").toString());
                            break;
                        }
                    }
                    if (coinId == 0) {
                        continue;
                    }

                    ticker.setUnitCode(unitCode);
                    if (tickerObject.get("highestBid") != null) {
                        String buy = tickerObject.get("highestBid").toString();
                        ticker.setBuy(Double.parseDouble(buy));
                    }
                    if (tickerObject.get("lowestAsk") != null) {
                        String sell = tickerObject.get("lowestAsk").toString();
                        ticker.setSell(Double.parseDouble(sell));
                    }
                    if (tickerObject.get("low") != null) {
                        String low = tickerObject.get("low").toString();
                        ticker.setLow(Double.parseDouble(low));
                    }
                    if (tickerObject.get("high") != null) {
                        String high = tickerObject.get("high").toString();
                        ticker.setHigh(Double.parseDouble(high));
                    }
                    if (tickerObject.get("last") != null) {
                        String last = tickerObject.get("last").toString();
                        ticker.setLast(Double.parseDouble(last));
                    }
                    if (tickerObject.get("baseVolume") != null) {
                        String vol = tickerObject.get("baseVolume").toString();
                        ticker.setVol(Double.parseDouble(vol));
                    }
                    ticker.setCoinId(coinId);
                    ticker.setSiteId(siteId);
                    if (ticker.getLast().doubleValue() != 0D) {
                        tickerDAO.insert(ticker);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void bittrex(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] units = new String[]{"btc", "eth", "usdt"};
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                for (String unit : units) {

                    String api = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=UNIT-COIN";
                    api = api.replace("COIN", coin.get("code").toString());
                    api = api.replace("UNIT", unit);
                    api = api.toLowerCase();
                    json = HttpsClientUtils.doGet(api);
                    if (json != null && !"".equals(json)) {
                        JSONObject object2 = JSON.parseObject(json);
                        JSONArray array =object2.getJSONArray("result");
                        if(array==null){
                            continue;
                        }
                        JSONObject object= array.getJSONObject(0);
                        if (object != null) {
                            String high = object.get("High").toString();
                            String low = object.get("Low").toString();
                            String vol = object.get("Volume").toString();
                            String buy = object.get("Bid").toString();
                            String sell = object.get("Ask").toString();
                            String last = object.get("Last").toString();
                            Ticker ticker = new Ticker();
                            ticker.setHigh(Double.parseDouble(high));
                            ticker.setLow(Double.parseDouble(low));
                            ticker.setVol(Double.parseDouble(vol));
                            ticker.setBuy(Double.parseDouble(buy));
                            ticker.setSell(Double.parseDouble(sell));
                            ticker.setLast(Double.parseDouble(last));
                            String unitCode = unit.substring(0, 3);
                            ticker.setUnitCode(unitCode.toUpperCase());
                            ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                            ticker.setSiteId(siteId);
                            tickerDAO.insert(ticker);
                        }

                    }
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void okex(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] units = new String[]{"btc", "eth", "usdt"};
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                for (String unit : units) {

                    String api = "https://www.okex.com/api/v1/ticker.do?symbol=COIN_UNIT";
                    api = api.replace("COIN", coin.get("code").toString());
                    api = api.replace("UNIT", unit);
                    api = api.toLowerCase();
                    json = HttpsClientUtils.doGet(api);
                    if (json != null && !"".equals(json)) {
                        JSONObject object2 = JSON.parseObject(json);
                        JSONObject object=object2.getJSONObject("ticker");

                        if (object != null) {
                            String high = object.get("high").toString();
                            String low = object.get("low").toString();
                            String vol = object.get("vol").toString();
                            String buy = object.get("buy").toString();
                            String sell = object.get("sell").toString();
                            String last = object.get("last").toString();
                            Ticker ticker = new Ticker();
                            ticker.setHigh(Double.parseDouble(high));
                            ticker.setLow(Double.parseDouble(low));
                            ticker.setVol(Double.parseDouble(vol));
                            ticker.setBuy(Double.parseDouble(buy));
                            ticker.setSell(Double.parseDouble(sell));
                            ticker.setLast(Double.parseDouble(last));
                            String unitCode = unit.substring(0, 3);
                            ticker.setUnitCode(unitCode.toUpperCase());
                            ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                            ticker.setSiteId(siteId);
                            tickerDAO.insert(ticker);
                        }

                    }
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void zb(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] units = new String[]{"btc", "eth", "usdt"};
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                for (String unit : units) {

                    String api = "http://api.zb.com/data/v1/ticker?market=COIN_UNIT";
                    api = api.replace("COIN", coin.get("code").toString());
                    api = api.replace("UNIT", unit);
                    api = api.toLowerCase();
                    json = HttpsClientUtils.doGet(api);
                    if (json != null && !"".equals(json)) {
                        JSONObject object2 = JSON.parseObject(json);
                        JSONObject object=object2.getJSONObject("ticker");

                        if (object != null) {
                            String high = object.get("high").toString();
                            String low = object.get("low").toString();
                            String vol = object.get("vol").toString();
                            String buy = object.get("buy").toString();
                            String sell = object.get("sell").toString();
                            String last = object.get("last").toString();
                            Ticker ticker = new Ticker();
                            ticker.setHigh(Double.parseDouble(high));
                            ticker.setLow(Double.parseDouble(low));
                            ticker.setVol(Double.parseDouble(vol));
                            ticker.setBuy(Double.parseDouble(buy));
                            ticker.setSell(Double.parseDouble(sell));
                            ticker.setLast(Double.parseDouble(last));
                            String unitCode = unit.substring(0, 3);
                            ticker.setUnitCode(unitCode.toUpperCase());
                            ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                            ticker.setSiteId(siteId);
                            tickerDAO.insert(ticker);
                        }

                    }
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void wex(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] units = new String[]{"btc", "eth", "usd"};
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                for (String unit : units) {

                    String api = "https://wex.nz/api/3/ticker/COIN_UNIT";
                    api = api.replace("COIN", coin.get("code").toString());
                    api = api.replace("UNIT", unit);
                    api = api.toLowerCase();
                    json = HttpsClientUtils.doGet(api);
                    if (json != null && !"".equals(json)) {
                        JSONObject object2 = JSON.parseObject(json);
                        JSONObject object= object2.getJSONObject(coin.get("code").toString().toLowerCase()+"_"+unit);
                        if (object != null) {
                            String high = object.get("high").toString();
                            String low = object.get("low").toString();
                            String vol = object.get("vol").toString();
                            String buy = object.get("buy").toString();
                            String sell = object.get("sell").toString();
                            String last = object.get("last").toString();
                            Ticker ticker = new Ticker();
                            ticker.setHigh(Double.parseDouble(high));
                            ticker.setLow(Double.parseDouble(low));
                            ticker.setVol(Double.parseDouble(vol));
                            ticker.setBuy(Double.parseDouble(buy));
                            ticker.setSell(Double.parseDouble(sell));
                            ticker.setLast(Double.parseDouble(last));
                            String unitCode = unit.substring(0, 3);
                            ticker.setUnitCode(unitCode.toUpperCase());
                            ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                            ticker.setSiteId(siteId);
                            tickerDAO.insert(ticker);
                        }

                    }
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void allcoin(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "https://api.allcoin.com/api/v1/ticker?symbol=";
        Ticker ticker = null;
        String json = "";
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] unitCodes = {"btc", "usd", "eth"};
        try {
            for (String unitCode : unitCodes) {
                for (Map<String, Object> coin : coins) {
                    String coinName = coin.get("code").toString();
                    api += coinName + "_" + unitCode;
                    json = HttpRequest.sendGet(api.toLowerCase(), null);
                    JSONObject object = JSON.parseObject(json);
                    String high = object.get("high").toString();
                    String last = object.get("last").toString();
                    String low = object.get("low").toString();
                    String vol = object.get("vol").toString();
                    String buy = object.get("buy").toString();
                    String sell = object.get("sell").toString();
                    ticker = new Ticker();
                    ticker.setHigh(Double.parseDouble(high));
                    ticker.setLow(Double.parseDouble(low));
                    ticker.setVol(Double.parseDouble(vol));
                    ticker.setBuy(Double.parseDouble(buy));
                    ticker.setSell(Double.parseDouble(sell));
                    ticker.setLast(Double.parseDouble(last));
                    ticker.setUnitCode(unitCode);
                    ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                    ticker.setSiteId(siteId);
                    tickerDAO.insert(ticker);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void kraken(Map<String, Object> site, List<Map<String, Object>> coins) {
        //暂时没有找到接口
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String json = "";
    }

    @Override
    public void gate(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "http://data.gate.io/api2/1/tickers";
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String json = HttpRequest.sendGet(api, null);
        if (!StringUtils.isEmpty(json)) {

            Map jsonMap = (Map) net.sf.json.JSONObject.toBean(net.sf.json.JSONObject.fromObject(json), Map.class);
            Iterator<Map.Entry<String, Object>> iterator = jsonMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                String symbol = entry.getKey();
                net.sf.ezmorph.bean.MorphDynaBean symbolValue = (net.sf.ezmorph.bean.MorphDynaBean) entry.getValue();
                Ticker ticker = new Ticker();
                String code = symbol.split("_")[0];
                String unitCode = symbol.replace("usdt", "usd").split("_")[1];
                Integer coinId = 0;
                for (Map<String, Object> coin : coins) {
                    if (code.equalsIgnoreCase(coin.get("code").toString())) {
                        coinId = Integer.parseInt(coin.get("id").toString());
                        break;
                    }
                }
                if (coinId == 0) {
                    continue;
                }
                ticker.setUnitCode(unitCode.toUpperCase());
                if (symbolValue.get("highestBid") != null) {
                    String buy = symbolValue.get("highestBid").toString();
                    ticker.setBuy(Double.parseDouble(buy));
                }
                if (symbolValue.get("lowestAsk") != null) {
                    String sell = symbolValue.get("lowestAsk").toString();
                    ticker.setSell(Double.parseDouble(sell));
                }
                if (symbolValue.get("low24hr") != null) {
                    String low = symbolValue.get("low24hr").toString();
                    ticker.setLow(Double.parseDouble(low));
                }
                if (symbolValue.get("high24hr") != null) {
                    String high = symbolValue.get("high24hr").toString();
                    ticker.setHigh(Double.parseDouble(high));
                }
                if (symbolValue.get("last") != null) {
                    String last = symbolValue.get("last").toString();
                    ticker.setLast(Double.parseDouble(last));
                }
                if (symbolValue.get("quoteVolume") != null) {
                    String vol = symbolValue.get("quoteVolume").toString();
                    ticker.setVol(Double.parseDouble(vol));
                }
                ticker.setCoinId(coinId);
                ticker.setSiteId(siteId);
                tickerDAO.insert(ticker);
            }
        }
    }


    @Override
    public void binance(Map<String, Object> site, List<Map<String, Object>> coins) {
        String api = "https://www.binance.com/api/v1/ticker/24hr";
        String json = HttpRequest.sendGet(api, null);
        Integer siteId = Integer.parseInt(site.get("id").toString());
        //List<String> symbols = (List<String>) jhjRedis.get("BINANCE_SYMBOLS");
        List<Ticker> tickers = new ArrayList<>();
        if (!StringUtils.isEmpty(json)) {
            try {
                List<JSONObject> array = JSON.parseArray(json, JSONObject.class);
                for (int i = 0; i < array.size(); i++) {
                    JSONObject tickerObject = array.get(i);
                    String symbol = tickerObject.getString("symbol");
                    Ticker ticker = new Ticker();
                    //取得币种名称，先替换USDT为USD，再截取
                    String tmp = symbol.replace("USDT", "USD");
                    String code = tmp.substring(0, tmp.length() - 3);
                    String unitCode = tmp.substring(tmp.length() - 3, tmp.length());
                    Integer coinId = 0;
                    for (Map<String, Object> map : coins) {
                        if (code.equalsIgnoreCase(map.get("code").toString())) {
                            coinId = Integer.parseInt(map.get("id").toString());
                            break;
                        }
                    }
                    if (coinId == 0) {
                        continue;
                    }
                    ticker.setUnitCode(unitCode);
                    if (tickerObject.get("bidPrice") != null) {
                        String buy = tickerObject.get("bidPrice").toString();
                        ticker.setBuy(Double.parseDouble(buy));
                    }
                    if (tickerObject.get("bidPrice") != null) {
                        String sell = tickerObject.get("bidPrice").toString();
                        ticker.setSell(Double.parseDouble(sell));
                    }
                    if (tickerObject.get("lowPrice") != null) {
                        String low = tickerObject.get("lowPrice").toString();
                        ticker.setLow(Double.parseDouble(low));
                    }
                    if (tickerObject.get("highPrice") != null) {
                        String high = tickerObject.get("highPrice").toString();
                        ticker.setHigh(Double.parseDouble(high));
                    }
                    if (tickerObject.get("lastPrice") != null) {
                        String last = tickerObject.get("lastPrice").toString();
                        ticker.setLast(Double.parseDouble(last));
                    }
                    if (tickerObject.get("volume") != null) {
                        String vol = tickerObject.get("volume").toString();
                        ticker.setVol(Double.parseDouble(vol));
                    }
                    ticker.setCoinId(coinId);
                    ticker.setSiteId(siteId);
                    tickerDAO.insert(ticker);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void huobipro(Map<String, Object> site, List<Map<String, Object>> coins) {
        Integer siteId = Integer.parseInt(site.get("id").toString());
        String[] units = new String[]{"btc", "eth", "usdt"};
        String json = "";
        try {
            for (Map<String, Object> coin : coins) {
                for (String unit : units) {

                    String api = "https://api.huobipro.com/market/detail/merged?symbol=COINUNIT";
                    api = api.replace("COIN", coin.get("code").toString());
                    api = api.replace("UNIT", unit);
                    api = api.toLowerCase();
                    json = HttpsClientUtils.doGet(api);
                    if (json != null && !"".equals(json)) {
                        JSONObject object2 = JSON.parseObject(json);
                        JSONObject object= object2.getJSONObject("tick");
                        if (object != null) {
                            String high = object.get("high").toString();
                            String low = object.get("low").toString();
                            String vol = object.get("vol").toString();
                            String buy = object.get("bid").toString();
                            String sell = object.get("ask").toString();
                            String last = object.get("close").toString();

                            Ticker ticker = new Ticker();
                            ticker.setHigh(Double.parseDouble(high));
                            ticker.setLow(Double.parseDouble(low));
                            ticker.setVol(Double.parseDouble(vol));
                            ticker.setBuy(Double.parseDouble(buy));
                            ticker.setSell(Double.parseDouble(sell));
                            ticker.setLast(Double.parseDouble(last));
                            String unitCode = unit.substring(0, 3);
                            ticker.setUnitCode(unitCode.toUpperCase());
                            ticker.setCoinId(Integer.parseInt(coin.get("id").toString()));
                            ticker.setSiteId(siteId);
                            tickerDAO.insert(ticker);
                        }

                    }
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
